"""
校园圈子管理后台配置
"""

from django.contrib import admin
from django.utils.html import format_html
from .models import Category, Post, PostImage, Comment, Like, Tag, PostTag


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    """
    分类管理
    """
    
    list_display = ['name', 'description', 'icon', 'post_count', 'created_at']
    list_filter = ['created_at']
    search_fields = ['name', 'description']
    ordering = ['-created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'description')
        }),
        ('样式设置', {
            'fields': ('icon',)
        }),
        ('状态设置', {
            'fields': ('is_active',)
        }),
    )
    
    def post_count(self, obj):
        """显示帖子数量"""
        return obj.posts.filter(is_active=True).count()
    post_count.short_description = '帖子数量'


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    """
    标签管理
    """
    
    list_display = ['name', 'color', 'post_count', 'created_at']
    list_filter = ['created_at']
    search_fields = ['name']
    ordering = ['-created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'color')
        }),
        ('状态设置', {
            'fields': ('is_active',)
        }),
    )
    
    def post_count(self, obj):
        """显示使用该标签的帖子数量"""
        return obj.posts.count()
    post_count.short_description = '帖子数量'


class PostImageInline(admin.TabularInline):
    """
    帖子图片内联管理
    """
    
    model = PostImage
    extra = 1
    fields = ['image', 'order']


class PostTagInline(admin.TabularInline):
    """
    帖子标签内联管理
    """
    
    model = PostTag
    extra = 1


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    """
    帖子管理
    """
    
    list_display = [
        'title', 'author', 'category', 'comment_count', 'like_count', 
        'is_top', 'status', 'created_at'
    ]
    list_filter = ['category', 'is_top', 'status', 'created_at']
    search_fields = ['title', 'content', 'author__username', 'author__nickname']
    ordering = ['-created_at']
    inlines = [PostImageInline, PostTagInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'content', 'author', 'category')
        }),
        ('状态设置', {
            'fields': ('is_top', 'is_active')
        }),
    )
    
    def comment_count(self, obj):
        """显示评论数量"""
        return obj.comments.filter(is_active=True).count()
    comment_count.short_description = '评论数'
    
    def like_count(self, obj):
        """显示点赞数量"""
        return obj.likes.count()
    like_count.short_description = '点赞数'
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('author', 'category')


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    """
    评论管理
    """
    
    list_display = [
        'content', 'author', 'post', 'parent', 'like_count', 
        'is_hidden', 'created_at'
    ]
    list_filter = ['is_hidden', 'created_at']
    search_fields = ['content', 'author__username', 'post__title']
    ordering = ['-created_at']
    
    fieldsets = (
        ('评论内容', {
            'fields': ('content', 'author', 'post', 'reply_to')
        }),
        ('状态设置', {
            'fields': ('is_active',)
        }),
    )
    
    def like_count(self, obj):
        """显示点赞数量"""
        return obj.likes.count()
    like_count.short_description = '点赞数'
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('author', 'post', 'reply_to')


@admin.register(Like)
class LikeAdmin(admin.ModelAdmin):
    """
    点赞管理
    """
    
    list_display = ['user', 'post', 'comment', 'created_at']
    list_filter = ['created_at']
    search_fields = ['user__username', 'post__title', 'comment__content']
    ordering = ['-created_at']
    
    fieldsets = (
        ('点赞信息', {
            'fields': ('user', 'post', 'comment')
        }),
    )
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('user', 'post', 'comment')


@admin.register(PostImage)
class PostImageAdmin(admin.ModelAdmin):
    """
    帖子图片管理
    """
    
    list_display = ['post', 'image_preview', 'order', 'created_at']
    list_filter = ['created_at']
    search_fields = ['post__title']
    ordering = ['post', 'order']
    
    fieldsets = (
        ('图片信息', {
            'fields': ('post', 'image', 'order')
        }),
    )
    
    def image_preview(self, obj):
        """显示图片预览"""
        if obj.image:
            return format_html(
                '<img src="{}" width="50" height="50" style="object-fit: cover;" />',
                obj.image.url
            )
        return "无图片"
    image_preview.short_description = '图片预览'
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('post')


@admin.register(PostTag)
class PostTagAdmin(admin.ModelAdmin):
    """
    帖子标签关联管理
    """
    
    list_display = ['post', 'tag', 'created_at']
    list_filter = ['created_at']
    search_fields = ['post__title', 'tag__name']
    ordering = ['-created_at']
    
    fieldsets = (
        ('关联信息', {
            'fields': ('post', 'tag')
        }),
    )
    
    def get_queryset(self, request):
        """优化查询"""
        return super().get_queryset(request).select_related('post', 'tag')
